package com.yksj.healthtalk.adapter;

import java.util.List;

import org.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.friend.FriendMainActivity;

public class FriendFindGridViewAdapter extends BaseAdapter {

	private final List<CustomerInfoEntity> mList;
	private final LayoutInflater mInflater;
	private final ImageLoader mImageLoader;
	private Context context;
	private Drawable unKownDrawable;
	private Drawable femalDrawable;
	private Drawable maleDrawable;
	private Fragment fragment;
	private int itemWidth;
	private boolean isNearBy= false;

	public FriendFindGridViewAdapter(Context mContext,
			List<CustomerInfoEntity> mList, Fragment fragment) {
		this.context = mContext;
		mInflater = LayoutInflater.from(mContext);
		this.mList = mList;
		this.fragment = fragment;
		mImageLoader = ImageLoader.getInstance();
		unKownDrawable = context.getResources().getDrawable(
				R.drawable.sex_unknown);
		femalDrawable = context.getResources().getDrawable(R.drawable.women);
		maleDrawable = context.getResources().getDrawable(R.drawable.man);
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		itemWidth = dm.widthPixels / 4 - 15;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	// 获取翻页的标记
	public String getPageMark(boolean isFristPage) {
		if (mList.size() > 0 && !isFristPage) {
			return mList.get(mList.size()-1).getFlag();
		} else {
			return "-100000";
		}
	}
	
	public void  setIsNearBy(Boolean isNearBy){
		this.isNearBy = isNearBy;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder mHolder;
		final CustomerInfoEntity mInfoEntity = mList.get(position);
		if (convertView == null) {
			mHolder = new ViewHolder();
			convertView = mInflater.inflate(
					R.layout.firend_seach_gridview_iterm, null);
			mHolder.mHeadeImage = (ImageView) convertView
					.findViewById(R.id.headimage);
			mHolder.mSex = (ImageView) convertView.findViewById(R.id.sex);
			mHolder.mName = (TextView) convertView.findViewById(R.id.name);
			mHolder.V = (ImageView)convertView.findViewById(R.id.v);
			convertView.setTag(mHolder);
			convertView.setLayoutParams(new AbsListView.LayoutParams(itemWidth, itemWidth));
		}else {
			mHolder = (ViewHolder) convertView.getTag();
		}
		mImageLoader.displayImage(mInfoEntity.getSex(),mInfoEntity.getNormalHeadIcon(),
				mHolder.mHeadeImage);
		if (isNearBy) {
			mHolder.mName.setText((int)mInfoEntity.getDistance()+"m");
		}else {
			mHolder.mName.setText(mInfoEntity.getName());
		}
		
		//是否是医师沙龙
		if (mInfoEntity.isShow() == true || mInfoEntity.isAudit() == true) {
			mHolder.V.setVisibility(View.VISIBLE);
		}else {
			mHolder.V.setVisibility(View.GONE);
		}

		mHolder.mHeadeImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				v.setTag(position);
				((FriendMainActivity) fragment).onClick(v);
			}
		});
		return convertView;
	}

	static class ViewHolder {
		ImageView mHeadeImage;
		ImageView mSex;
		TextView mName;
		ImageView V;
	}
}
